package com.parse.tree.parsers.java;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.commons.Constants;
import com.commons.ParsingResult;
import com.parse.tree.parsers.service.java.JavaParsingService;

import dao.TagDAO;

public class ParsingServiceTest {

	ApplicationContext appContext = new FileSystemXmlApplicationContext(
			"src/main/java/resources/applicationContext.xml");

	// @Autowired
	// TagDAO dao;
	TagDAO dao = (TagDAO) appContext.getBean("tagDao");

	public void parse(String dirName, String parserType) {
		JavaParsingService parsingService = new JavaParsingService();
		System.out.println("############## Parsing Starts ##############");
		try {

			if (parserType.equalsIgnoreCase("g")) {
				// Parsing service for g
				// ParsingService parsingService = new ParsingService();
			}

			Map<String, ParsingResult> parseDirectoryResult;

			parseDirectoryResult = parsingService.parseDirectory(dirName);

			System.out.println("############## Parsing Ends ##############");
			Set<Entry<String, ParsingResult>> entrySet = parseDirectoryResult
					.entrySet();
			for (Entry<String, ParsingResult> resultPerFile : entrySet) {
				System.out.println("\nResult for file name:"
						+ resultPerFile.getKey());
				System.out.println(resultPerFile.getValue() + "\n");
				System.out
						.println("#################################################");
			}

			for (Entry<String, ParsingResult> resultPerFile : entrySet) {

				ParsingResult result = resultPerFile.getValue();
				String fileName = resultPerFile.getKey();
				for (String keyword : result.getKeywordList()) {
					dao.tagProcessing(keyword, Constants.Keyword_ID, fileName);
				}
				for (String type : result.getTypeNodes()) {
					dao.tagProcessing(type, Constants.Datatype_ID, fileName);
				}
				for (String var : result.getVariableList()) {
					dao.tagProcessing(var, Constants.Variable_ID, fileName);
				}
				for (String string : result.getStringNodes()) {
					dao.tagProcessing(string, Constants.String_ID, fileName);
				}
				for (String strchar : result.getStrcharNodes()) {
					dao.tagProcessing(strchar, Constants.Strchar_ID, fileName);
				}
			}
			
			//Call to word cloud
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}

	public static void main(String srgs[]) throws Throwable {
		ParsingServiceTest p = new ParsingServiceTest();
		p.parse("/Users/macpro/aad-webservice", "java");
	}

}
